//
//  Constant.swift
//  LeTu
//
//  Created by gy on 2023/11/24.
//

import Foundation

@_exported import ObjectMapper
@_exported import Alamofire
@_exported import Moya
@_exported import RxSwift
@_exported import RxCocoa
@_exported import SnapKit
@_exported import JXSegmentedView
@_exported import AttributedString


struct Constant {
    
    /// 版本号
    static let versionCode = 20240824
    
    /// 商店地址
    static let kAppStoreUrl = "https://apps.apple.com/cn/app/id6473636037"
    
}


/// 设备是否为iPhoneX
let iPhoneX = screenHeight >= 812 ? true : false

/// 设备是否为pad
let kIsPad: Bool = {
    return UIDevice.current.userInterfaceIdiom == .pad
}()

/// 设备是否为iphone
let kIsPhone: Bool = {
    return UIDevice.current.userInterfaceIdiom == .phone
}()

/// 是否为竖屏
let kIsPortrait: Bool = {
    let size = CGSize(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
    return size.height > size.width
}()

/// 是否有刘海儿
var IsHaveHair: Bool {
    if #available(iOS 11, *) {
          guard let w = UIApplication.shared.delegate?.window, let unwrapedWindow = w else {
              return false
          }
          if unwrapedWindow.safeAreaInsets.left > 0 || unwrapedWindow.safeAreaInsets.bottom > 0 {
              return true
          }
    }
    return false
}

public enum AppCellRectCorner {
    case none
    case top
    case bottom
    case left
    case right
    case all
}

// MARK: -  UIColor
func kSetHexColor(hex: Int) -> UIColor {
    return UIColor(hex: hex)
}

// RGB设置
func kSetRGB(r:CGFloat, g:CGFloat, b:CGFloat) -> UIColor {
    return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1.0)
}
func kSetRGBA(r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
    return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
// 十六进制
func RGB0X(hexValue: Int) -> (UIColor) {
    return HEXA(hexValue: hexValue, a: 1.0)
}
func HEXA(hexValue: Int, a: CGFloat) -> (UIColor) {
    return UIColor(red: ((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0,
                   green: ((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0,
                   blue: ((CGFloat)(hexValue & 0xFF)) / 255.0,
                   alpha: a)
}


// MARK: -  UIFont - PingFang
func kSetPingFangRegular(_ font: CGFloat, isScale: Bool = true) -> UIFont {
    return UIFont(name: "PingFang-SC-Regular", size: font*(isScale ? kAppThemeManager.fontScale:1)) ?? UIFont.systemFont(ofSize: font)
}
func kSetPingFangMedium(_ font: CGFloat, isScale: Bool = true) -> UIFont {
    return UIFont(name: "PingFang-SC-Medium", size: font*(isScale ? kAppThemeManager.fontScale:1)) ?? UIFont.systemFont(ofSize: font)
}
func kSetPingFangLight(_ font: CGFloat, isScale: Bool = true) -> UIFont {
    return UIFont(name: "PingFang-SC-Light", size: font*(isScale ? kAppThemeManager.fontScale:1)) ?? UIFont.systemFont(ofSize: font)
}
func kSetPingFangBlod(_ font: CGFloat, isScale: Bool = true) -> UIFont {
    return UIFont(name: "PingFang-SC-Semibold", size: font*(isScale ? kAppThemeManager.fontScale:1)) ?? UIFont.systemFont(ofSize: font)
}
func kSetSmileySans(_ font: CGFloat, isScale: Bool = true) -> UIFont {
    return UIFont(name: "SmileySans-Oblique", size: font*(isScale ? kAppThemeManager.fontScale:1)) ?? UIFont.systemFont(ofSize: font)
}

// MARK: -  Size
struct Size {
    
    static var statusBarHeight: CGFloat {
        return safeAreaTopGap > 0 ? safeAreaTopGap : 20
    }
    static let navigationBarWithoutStatusHeight: CGFloat = 44
    static let navigationBarHeight: CGFloat = navigationBarWithoutStatusHeight + statusBarHeight
    static let tabBarHeight: CGFloat = 49 + 1 / UIScreen.main.scale
    static var safeAreaTopGap: CGFloat {
        if #available(iOS 11.0, *) {
            let window = UIApplication.shared.keyWindow
            
            return window?.safeAreaInsets.top ?? 0
        } else {
            return 0
        }
    }
    static var safeAreaBottomGap: CGFloat {
        if #available(iOS 11.0, *) {
            let window = UIApplication.shared.keyWindow
            return window?.safeAreaInsets.bottom ?? 0
        } else {
            return 0
        }
    }
    static let onePixel = 1 / UIScreen.main.scale
    
    static let screenMaxLength = max(screenWidth, screenHeight)

}

// MARK: ----- UI
extension UIView {
    func layerCornerRadius(_ radius: CGFloat,_ corners: UIRectCorner = [.allCorners]) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
    }
}
extension Optional {
    // Unwrap optional value for printing log only
    var description: String {
        if let self = self {
            return "\(self)"
        }
        return ""
    }
}

// MARK: - Log
func MYLog(_ message: Any..., fileName: String = #file, funcName: String = #function, lineNum: Int = #line) {
    
    #if DEBUG
    
    let file = (fileName as NSString).lastPathComponent
        
    print("[\(file):\(lineNum)--->] - \(message)")

    #endif
    
}



